<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目标段" prop="institutionCode">
        <el-select
          v-model="queryParams.institutionCode"
          placeholder="请选择"
          size="small"
          style="width: 240px" @change=""
        >
          <el-option-group
            v-for="group in projectBidOptions"
            :key="group.name"
            :label="group.name">
            <el-option
              v-for="item in group.bids"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['site:video:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['site:video:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['site:video:remove']"
        >删除</el-button>
      </el-col>
      <el-button
        type="primary"
        plain
        icon="el-icon-setting"
        size="mini"
        @click="handleSetting"
      >场景设置</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标段名称" align="center" prop="institutionName" />
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="设备地址" align="center" prop="address" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="loginUser == 'admin'">
            <el-switch
              v-model="scope.row.status"
              active-value="0"
              inactive-value="1"
            ></el-switch>
          </div>
          <div v-else>
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="handleVideo(scope.row)"
          >播放</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频监控对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="项目标段" prop="institutionCode">
              <el-select
                v-model="form.institutionCode"
                placeholder="请选择"
                size="small"
                style="width: 240px" @change=""
              >
                <el-option-group
                  v-for="group in projectBidOptions"
                  :key="group.name"
                  :label="group.name">
                  <el-option
                    v-for="item in group.bids"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code">
                  </el-option>
                </el-option-group>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name">
              <el-input type="textarea" v-model="form.name" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备地址" prop="address">
              <el-input type="textarea" v-model="form.address" placeholder="请输入设备地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="高清地址" prop="urlLiveHd">
              <el-input type="textarea" v-model="form.urlLiveHd" placeholder="请输入高清地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标清地址" prop="urlLive">
              <el-input type="textarea" v-model="form.urlLive" placeholder="请输入标清地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="FLS高清" prop="urlHlsHd">
              <el-input type="textarea" v-model="form.urlHlsHd" placeholder="请输入FLS高清" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="FLS标清" prop="urlHls">
              <el-input type="textarea" v-model="form.urlHls" placeholder="请输入FLS标清" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="FLV高清" prop="urlFlvHd">
              <el-input type="textarea" v-model="form.urlFlvHd" placeholder="请输入FLV高清" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="FLV标清" prop="urlFlv">
              <el-input type="textarea" v-model="form.urlFlv" placeholder="请输入FLV标清" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="RTMP高清" prop="urlRtmpHd">
              <el-input type="textarea" v-model="form.urlRtmpHd" placeholder="请输入RTMP高清" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="RTMP标清" prop="urlRtmp">
              <el-input type="textarea" v-model="form.urlRtmp" placeholder="请输入RTMP标清" />
            </el-form-item>
          </el-col>
        </el-row>
    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <videoPlayer ref="videoPlayer"></videoPlayer>

  </div>
</template>

<script>
import { listVideo, getVideo, delVideo, addVideo, updateVideo } from "@/api/site/video/video";
import { selectProjectBidBranch } from "@/api/project/bid";
import videoPlayer from "@/views/site/video/video";

export default {
  name: "siteVideo",
  dicts: ['sys_normal_disable'],
  components: { videoPlayer },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频监控表格数据
      videoList: [],
      // 当前登录用户
      loginUser: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 标段下拉框
      projectBidOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        institutionCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        institutionCode: [
          { required: true, message: "项目标段不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "设备地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.loginUser = this.$store.state.user.name;
    console.log(this.loginUser);
    this.getList();
    this.initBidSelect();
  },
  methods: {
    /** 初始化标段下拉框 */
    initBidSelect() {
      selectProjectBidBranch().then(response => {
        this.projectBidOptions = response.data;
      });
    },
    /** 查询视频监控列表 */
    getList() {
      this.loading = true;
      listVideo(this.queryParams).then(response => {
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        institutionCode: null,
        name: null,
        address: null,
        status: "0",
        urlLiveHd: null,
        urlLive: null,
        urlHlsHd: null,
        urlHls: null,
        urlFlvHd: null,
        urlFlv: null,
        urlRtmpHd: null,
        urlRtmp: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频监控";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVideo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改视频监控";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVideo(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delVideo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    handleVideo(row) {
      this.$refs.videoPlayer.show(row.id);
    },
    handleSetting() {
      this.$router.push({ path: '/site/video/setting' })
    }
  }
};
</script>
